package cn.imu.appdevexperiment2020.work04;

import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.base.BaseActivity;
import cn.imu.appdevexperiment2020.utils.ActivityUtil;
import cn.imu.appdevexperiment2020.utils.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);//线性布局
        //登陆按钮被点击
        findViewById(R.id.login).setOnClickListener(view -> {
            Map<String, String> mp = new HashMap<>();

            TextView username = findViewById(R.id.editTextTextPersonName2);
            TextView password = findViewById(R.id.editTextTextPersonName3);


            mp.put("userName", username.getText().toString());
            mp.put("password", password.getText().toString());

            HttpUtil.post(HttpUtil.ADDR + "/app/user/api/login", mp, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    JSONObject jsonObject = JSON.parseObject(response.body().string());
                    if (jsonObject.get("code").toString().equals("200")) {
                        //说明解析成功跳转界面
                        ActivityUtil.getInstance().goActivity(LoginActivity.this, UserInfoActivity.class);

                    } else {
                        // System.out.println(jsonObject.get("code"));
                        //System.out.println("--------");
                    }

                }
            });

        });


        findViewById(R.id.button3).setOnClickListener(view -> {
            Map<String, String> mp = new HashMap<>();
            // TextView username= findViewById(R.id.editTextTextPersonName2);
            TextView password = findViewById(R.id.editTextTextPersonName3);

            mp.put("userId", "77");
            mp.put("password", password.getText().toString());
            mp.put("updateBy", "内大考研");


            HttpUtil.post(HttpUtil.ADDR + "/app/user/api/resetPwd", mp, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    System.out.println(response.body().string());
                }
            });

        });


        ////作业四

//        findViewById(R.id.button4).setOnClickListener((view -> {
//
//            Map<String, String> mp = new HashMap<>();
//            mp.put("userId", "77");
//            mp.put("userName", "32209128");
//            mp.put("password", "123456");
//            mp.put("nickName", "胡XX测试");
//            mp.put("sex", "0");
//            mp.put("status", "0");
//            mp.put("updateBy", "appname");
//            mp.put("phonenumber", "15614177688");
//            System.out.println("===>");
//            HttpUtil.get(HttpUtil.ADDR + "/classes/api/getClasses?classesId=130", new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    System.out.println(response.body().string());
//                }
//            });
//
//
//        }));

//        findViewById(R.id.button4).setOnClickListener((view -> {
//
//            Map<String,String > mp=new HashMap<>();
//            mp.put("userId","77");
//            mp.put("userName","32209128");
//            mp.put("password","123456");
//            mp.put("nickName","胡XX测试");
//            mp.put("sex","0");
//            mp.put("status","0");
//            mp.put("updateBy","appname");
//            mp.put("phonenumber","15614177688");
//            System.out.println("===>");
//            HttpUtil.post(HttpUtil.ADDR + "/app/user/api/edit",mp, new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                }
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    System.out.println(response.body().string());
//                }
//            });
//
//
//
//        }));

    }
}
